package io.halo.payment.jsapi.ctrl;

import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 微信支付Demo
 *
 * @author: austin
 * @since: 2023/1/11 15:52
 */
@Slf4j
@RestController
@RequestMapping("/wxpay")
public class WxPayDemoController {

    @Resource
    private WxPayService wxService;

    /**
     * 统一下单（详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1）
     * 在发起微信支付前，需要调用统一下单接口，获取预支付交易会话标识prepay_id
     * <p>
     * 接口URL地址：https://api.mch.weixin.qq.com/pay/unifiedorder
     */
    @PostMapping("/unifiedOrder")
    @ApiOperation(value = "原生的统一下单接口")
    public WxPayUnifiedOrderResult unifiedOrder(@RequestBody WxPayUnifiedOrderRequest request) throws WxPayException {
        return this.wxService.unifiedOrder(request);
    }

    @PostMapping("/unifiedOrderV3")
    @ApiOperation(value = "统一下单接口（最新V3版本）")
    public WxPayUnifiedOrderV3Result unifiedOrderV3(TradeTypeEnum tradeTypeEnum, WxPayUnifiedOrderV3Request request) throws WxPayException {
        return this.wxService.unifiedOrderV3(tradeTypeEnum, request);
    }


}
